<?php
class Locality_Featured_Property extends WP_Widget {

	function Locality_Featured_Property(){
		$widget_ops = array( 'classname' => 'Locality_Featured_Property', 'description' => __('Displays Random or Recent Featured Property','locality') );
		$this->WP_Widget( 'Locality_Featured_Property', 'Locality Featured Property', $widget_ops );
	}
	
	
	function widget($args, $instance) { 
		
		extract($args);
						
		$title = apply_filters('widget_title', $instance['title']);		
		
		if ( empty($title) ) $title = false;	
		
		$count = intval( $instance['count']);			
		$sort_by = $instance['sort_by'];	
		
		
		$args = array(
			'post_type' => 'property',
			'posts_per_page' => $count,
			'meta_query' => array(
								array(
									'key' => 'locality_featured',
									'value' => '1',
									'compare' => 'LIKE'
								)
							)
			);
		
		//Order by
		if($sort_by == "random"):
			$args['orderby']= "rand";			
		else:
			$args['orderby']= "date";			
		endif;			
		
		$featured_query = new WP_Query($args);
		
		echo $before_widget;
		
		if($featured_query->have_posts()): 					
					
			?>	
	                	<div class="featured-property">
                                <?php
                                if($title):
									echo $before_title;
										echo $title;
									echo $after_title;	
								endif;
								
								$i = 1;
								while($featured_query->have_posts()): 
			
									$featured_query->the_post();
								
									global $post;
									?>
		                            <div class="property-view">
		                                    <?php 
												if ( has_post_thumbnail() ) 
												{								
													?>
														<a href="<?php the_permalink(); ?>">
															<?php the_post_thumbnail('property-post-thumb');?>
														</a>
													<?php
												}
											?>
		                                    <div class="featured-price-tag">
		                                            <?php 	
													$terms = get_the_terms( $post->ID,"property-type" );
															
													if ( $terms && ! is_wp_error( $terms ) ) 
													{														   														   
														   foreach ( $terms as $term )
														   {															   
																echo '<small><a class="property-type" href="' . get_term_link($term->slug, "property-type" ) .'">'.$term->name.'</a></small>';
																break; // to display only one property type
														   }														   													   														   
													}												
													?>
		                                            <h5 class="price"><?php theme_currency(); echo number_format(intval(get_post_meta($post->ID, 'locality_property_price', true))); ?></h5>
		                                    </div>
		                            </div>
		                            <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
		                            <p><?php locality_excerpt(12); ?></p>  
									<?php
									if($i < $count)
									{
										?>
										<hr/>
										<?php
									}
									$i++;
								endwhile; 
								?>
	                    </div>		                         						
				<?php					
			wp_reset_query();
		else:
			?>
			<div class="featured-property">
				<?php
					echo $before_title;
						_e('No Featured Property Found!', 'locality');
					echo $after_title;	
				?>
			</div>
			<?php	
		endif;
		
		echo $after_widget;
	}
	

	function form($instance) 
	{	
		$instance = wp_parse_args( (array) $instance, array( 'title' => 'Featured Property', 'count' => 1 , 'sort_by' => 'random' ) );
	
        $title= esc_attr($instance['title']);	
		$count =  $instance['count'];	
		$sort_by = $instance['sort_by'];
		
		?>
			<p>
	            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'locality'); ?></label>
	            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
	        </p>
			<p>
					<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Properties Count', 'locality'); ?></label>
					<input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" />
			</p>    
			<p> 
				<label for="<?php echo $this->get_field_id('sort_by'); ?>"><?php _e('Sort By:', 'locality') ?></label>
				<select name="<?php echo $this->get_field_name('sort_by'); ?>" id="<?php echo $this->get_field_id('sort_by'); ?>" class="widefat">
						<option value="recent"<?php selected( $instance['sort_by'], 'recent' ); ?>><?php _e('Most Recent', 'locality'); ?></option>
						<option value="random"<?php selected( $instance['sort_by'], 'random' ); ?>><?php _e('Random', 'locality'); ?></option>
				</select>
			</p>			
		<?php
	}

	function update($new_instance, $old_instance) 
	{
        $instance = $old_instance;		
		
        $instance['title'] = strip_tags($new_instance['title']);
		$instance['count'] = $new_instance['count'];
		$instance['sort_by'] = $new_instance['sort_by'];
		
        return $instance;

    }
	
}